/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/cupertino.dart';
import 'package:xml/xml.dart';

import '../common/test_page.dart';

class ExceptionsTestPage extends TestPage {
  ExceptionsTestPage(String title, {Key? key}) : super(title, key: key) {
    group('XmlParentException', () {
      test('checkNoParent, 此处应该为X', () {
        final document = XmlDocument([XmlComment('Comment')]);
        XmlParentException.checkNoParent(document);
        XmlParentException.checkNoParent(document.firstChild!);
        // expect(
        //     () => XmlParentException.checkNoParent(document.firstChild!),
        //     throwsA(isXmlParentException(
        //       message: 'Node already has a parent, copy or remove it first',
        //       node: document.firstChild,
        //       parent: document,
        //     )));
      });
      test('checkMatchingParent, 此处应该为X', () {
        final document = XmlDocument([XmlComment('Comment')]);
        XmlParentException.checkMatchingParent(document.firstChild!, document);
        XmlParentException.checkMatchingParent(document, document.firstChild!);
      });
    });
    group('XmlParserException', () {
      test('with properties, 此处应该为X', () {
        final exception = XmlParserException('Expected foo', buffer: 'hello\nworld', position: 6);
        throw exception;
      });
      test('without anything, 此处应该为X', () {
        final exception = XmlParserException('Expected foo');
        throw exception;
      });
    });
    group('XmlNodeTypeException', () {
      test('checkValidType, 此处应该为X', () {
        final commentNode = XmlComment('Comment');
        final commentNodeTypes = [XmlNodeType.COMMENT];
        final otherNodeTypes = [XmlNodeType.ELEMENT, XmlNodeType.TEXT];
        XmlNodeTypeException.checkValidType(commentNode, commentNodeTypes);
        XmlNodeTypeException.checkValidType(commentNode, otherNodeTypes);
      });
    });
    group('XmlTagException', () {
      test('mismatchClosingTag, 此处应该为X', () {
        final exception = XmlTagException.mismatchClosingTag('foo', 'bar', buffer: '<foo>\n</bar>', position: 6);
        expect(exception, null);
        throw exception;
      });
      test('unexpectedClosingTag, 此处应该为X', () {
        final exception = XmlTagException.unexpectedClosingTag('bar', buffer: '</bar>', position: 0);
        throw exception;
      });
      test('missingClosingTag, 此处应该为X', () {
        final exception = XmlTagException.missingClosingTag('foo', buffer: '<foo>', position: 5);

        throw exception;
      });
      test('checkClosingTag, 此处应该为X', () {
        XmlTagException.checkClosingTag('foo', 'bar', buffer: '<foo>\n</bar>', position: 6);
        expect(() => XmlTagException.checkClosingTag('foo', 'bar', buffer: '<foo>\n</bar>', position: 6), null);
      });
    });
  }
}
